package com.ruoyi.service.masterdate.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.mapper.masterdate.SupplierMapper;
import com.ruoyi.service.masterdate.SupplierService;
import com.ruoyi.util.HttpClientUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@DataSource(value = DataSourceType.SLAVE)
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierMapper supplierMapper;

    @Override
    public List<Map<String, Object>> findSupplierAll(Map<String, Object> map) {
        return supplierMapper.findSupplierAll(map);
    }

    @Override
    public Map<String, Object> findSupplier(String id) {
        return supplierMapper.findSupplier(id);
    }

    @Override
    public int addSupplier(Map<String, Object> map) {
        return supplierMapper.addSupplier(map);
    }

    @Override
    public int editSupplier(Map<String, Object> map) {
        return supplierMapper.editSupplier(map);
    }

    @Override
    public boolean checkAccCode(String accCode) throws Exception {
        String url = "https://ccdcapi.alipay.com/validateAndCacheCardInfo.json";
        Map paramHeader = new HashMap();
        paramHeader.put("Content-Type", "application/x-www-form-urlencoded");
        Map paramBody = new HashMap();
        paramBody.put("_input_charset", "utf-8");
        paramBody.put("cardNo", accCode);
        paramBody.put("cardBinCheck", "true");
        String respstr = HttpClientUtils.doPost(null, url, paramHeader, paramBody, "utf-8");
        boolean flag = JSONObject.parseObject(respstr).getBoolean("validated");
        return flag;
    }
}
